<?php

namespace App\Model;

use App\Model\BaseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2021/11/6
 * Time: 15:29
 */

class UserUuidLiangLog extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'user_uuid_liang_check_log';

    public function updateUidStatus($user_id) {
        return DB::connection($this->connection)->table($this->table)
            ->where('status',1)
            ->where('user_id',$user_id)
            ->update(['status'=>2]);
    }
    public function setUidLog($token,$user_id,$old_uuid,$uuid,$end_date,$status){

        $users = $token?Users::findByToken($token):0;
        $data = [
            'user_id'   => $user_id,
            'old_uuid'   => $old_uuid,
            'new_uuid'   => $uuid,
            'created_at'   => time(),
            'end_at'   => strtotime($end_date),
            'status'   => 1,
            'opt_status'   => $status,
            'opt_id'   => $users?$users->id:0,
        ];
//        var_dump($data);die();
        return DB::connection($this->connection)->table($this->table)->insert($data);
    }
    public function getUuidList($id){
         $res = DB::connection($this->connection)->table($this->table)->where('user_id',$id)
             ->leftJoin('users',$this->table.'.opt_id','=','users.id')
             ->orderBy($this->table.'.created_at','desc')
             ->get([$this->table.'.*','users.name']);
         foreach ($res as $item){
             $item->created_at = date('Y-m-d H:i:s',$item->created_at);
             $item->end_at = date('Y-m-d H:i:s',$item->end_at);
             $item->status = $item->opt_status == 1 ? "自动变更" :"手动变更" ;
         }
        return $res;
    }

}
